package com.novax.ex.activity.provider.controller;

import com.alibaba.fastjson.JSON;
import com.novax.ex.activity.infrastructure.entity.MallBanner;
import com.novax.ex.activity.infrastructure.entity.MallProduct;
import com.novax.ex.activity.infrastructure.mapper.MallProductMapper;
import com.novax.ex.activity.open.api.MallPrizeApi;
import com.novax.ex.activity.open.model.query.MallWinnerQuery;
import com.novax.ex.activity.open.model.query.ShareContentQuery;
import com.novax.ex.activity.open.model.response.*;
import com.novax.ex.activity.provider.handler.provider.MallProductProvider;
import com.novax.ex.activity.provider.service.*;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import com.novax.ex.common.util.PageQuery;
import com.novax.ex.common.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author zhaolei
 * @Date 2/22/23 2:37 PM
 * @Version 1.0
 */
@Slf4j
@RestController
public class MallPrizeController extends BaseActivityController implements MallPrizeApi {
    @Resource
    private MallPrizeService mallPrizeService;
    @Resource
    private MallPrizeOrderService mallPrizeOrderService;
    @Resource
    private MallOrderWinnerService mallOrderWinnerService;
    @Resource
    private MallServiceLanguageService mallServiceLanguageService;
    @Resource
    private MallBannerService mallBannerService;
    @Resource
    private MallProductMapper mallProductMapper;
    @Resource
    private MallProductProvider mallProductProvider;

    @Override
    public ReturnResult<PageUtil<MallPrizeResponse>> list(String language, String token, PageQuery pageQuery) {
        return mallPrizeService.list(language, token, pageQuery);
    }

    @Override
    public ReturnResult<List<MallMsgResponse>> msgList(String language, Integer type) {
        if (type == 1) {
            return ReturnResult.success(mallPrizeOrderService.msgList(language));
        }
        if (type == 2) {
            return ReturnResult.success(mallOrderWinnerService.msgList(language));
        }
        return ReturnResult.success();
    }

    @Override
    public ReturnResult<MallPrizeResponse> detail(String language ,String token, Long id) {
        return ReturnResult.success(mallPrizeService.detail(language,token, id));
    }

    @Override
    public ReturnResult<MallServiceLanguageResponse> service(String language) {
        return ReturnResult.success(mallServiceLanguageService.service(language));
    }

    @Override
    public ReturnResult<MallServiceLanguageResponse> test(String type, Long id) {
        /*if(type.equals("1")){
            MallProduct mallProduct =  mallProductMapper.selectByPrimaryKey(id);
            mallProductProvider.sendPutOn(JSON.toJSONString(mallProduct));
        }
        if(type.equals("2")){
            MallProduct mallProduct =  mallProductMapper.selectByPrimaryKey(id);
            mallProductProvider.sendDel(JSON.toJSONString(mallProduct));
        }
        if(type.equals("3")){
            MallProduct mallProduct =  mallProductMapper.selectByPrimaryKey(id);
            mallProductProvider.sendUpdatePrice(JSON.toJSONString(mallProduct));
        }
        if(type.equals("4")){
            MallProduct mallProduct =  mallProductMapper.selectByPrimaryKey(id);
            mallProductProvider.sendUpdateCost(JSON.toJSONString(mallProduct));
        }*/
        return null;
    }

    @Override
    public ReturnResult<List<MallMsgResponse>> msgDetailList(String language, Long mallProductId) {
        return ReturnResult.success(mallOrderWinnerService.msgDetailList(language,mallProductId));
    }

    @Override
    public ReturnResult<List<MallBannerResponse>> banners(String language) {
        List<MallBanner> list = mallBannerService.queryList(language);
        List<MallBannerResponse> mallBannerResponses = CopyUtils.copyList(list, MallBannerResponse.class);
        return ReturnResult.success(mallBannerResponses);
    }

    @Override
    public ReturnResult<MallPrizeResponse> detailByProductId(String language, String token, Long productId) {
        return ReturnResult.success(mallPrizeService.detailByProductId(language,token, productId));
    }

    @Override
    public ReturnResult<PageUtil<MallOrderWinnerResponse>> winners(String language, MallWinnerQuery pageQuery) {
        PageUtil<MallOrderWinnerResponse> pages = new PageUtil<>(pageQuery);
        Map<String, Object> condition = new HashMap<>();
        condition.put("query", pageQuery);
        pages.setCondition(condition);
        mallOrderWinnerService.winnersPage(pages,MallOrderWinnerResponse.class);
        List<MallOrderWinnerResponse> items = pages.getItems();
        if(!CollectionUtils.isEmpty(items)){
            for (MallOrderWinnerResponse item : items) {
                item.setNickname(MallPrizeOrderService.getNickName(item.getNickname()));
            }
        }
        return ReturnResult.success(pages);
    }
}
